import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const state = {
  num: 0,
  ws: null,
  loading: false,
  msgList: [],
  isStartScan: false,
  msgIndex: 0,
  messageContent : "",
  conveyor_state : [],
  showText : "",
  imgCalibration: "",
  createFileLoading: false,
  // 涂胶参数设置

  //显示日志板
  showLogBoard: false,

};

const mutations = {
  addNum(){
    state.num++
  },
  initWs(){
     state.ws = new WebSocket('ws://127.0.0.1:8000/test/test_ws_srv')
            state.ws.onopen = () => {

            };
            state.ws.onmessage = (e) => {
              let message = JSON.parse(e.data)
              if (message.component === "msgBox" && message.content === "Give me number"){
                state.ws.send(state.msgIndex)
                return true
              }
              if (message.component === "msgBox"){
                state.msgList.push(message)
              }else if (message.component === 'threeD'){
                if (message.content === "unload"){
                  state.loading = false;
                  return false
                }
                switch(message.code){
                  case 40001:
                    this.$message({
                      type: 'success',
                      message: message.message,
                      showClose: true,
                    });
                   break;
                  case 40002:
                    if (message.content === state.messageContent){
                      state.loading = false
                    }
                    state.messageContent = message.content
                    //state.loading = false
                    state.msgList.push({
                      'component': 'msgBox',
                      'id': 0,
                      'content': 'Loaded successfully',
                      'msgLevel': 'success',
                      'code': -1,
                    });
                    state.isStartScan = false
                    break;
            }
              }
              if (message.component === 'createFile'){
                state.showText = message.content.split('\n');
              }
              if(message.component === "setting-calibrate"){
                state.imgCalibration = message.content;
              }
              state.msgIndex++
            };
  },
  activeCalibrateState(){
    state.createFileLoading = true
  },
  sleepCalibrateState(){
    state.createFileLoading = false
  },
  activeLoadState(){
    state.loading = true
  },
  sleepLoadState(){
    state.loading = false
  },
  activeScanState(){
    state.isStartScan = true
  },
  sleepScanState(){
    state.isStartScan = false
  },
  resetMessageContent(){
    state.messageContent = ""
  },
  resetMessageList(){
    state.msgList = []
  },
  clearShowText(){
    state.showText = ""
  },
  activeShowLogBoard(){
    state.showLogBoard = !state.showLogBoard;
    console.log(state.showLogBoard)
  }
};
const actions = {
  addNumAction(context, num){
    context.commit('addNum', num)
  },
  initWsAction(context, ws){
    context.commit('initWs', ws)
  },
  activeLoadStateAction(context, loading){
    context.commit('activeLoadState', loading)
  },
  sleepLoadStateAction(context, loading){
    context.commit('sleepLoadState', loading)
  }
};
const store = new Vuex.Store({
  state,
  mutations,
  actions,
});
export default store
